<!DOCTYPE html>
<html>
  <head>
  <script type="text/javascript" src="js/hero.js"></script>
  <script type="text/javascript">
  API.boot = function(data){
  }
  API.special_logic = function(data){
    if (data.click == 'query') {
      API.out({command:'showLoading'});
      xhr({
          url:('/replay'),
          data:{phone:ui2Data.phone},
          async:true,
          error:function(){
            API.out({command:'stopLoading'})
          },
          success:function(data){
            API.out({command:'stopLoading'})
            API.logs = [];
            var arr = JSON.parse(data);
            arr = arr.queryResult.result
            for (var i = 0; i < arr.length; i++) {
              var arr1Object = arr[i];
              var body = arr[i].body;
              body = body.replace('\n','');
              var arr1 = JSON.parse(body);
              if (arr1 && arr1.log) {
                var logStrs = arr1.log;
                for (var j = 0; j < logStrs.length; j++) {
                  var logStr = logStrs[j];
                  logStr = logStr.replace('\n','');
                  try{
                    var arr2 = JSON.parse('['+logStr+']');
                  }catch(e){
                    API.out({datas:{name:'console',append:'无法解析日志: '+logStr}});
                  }
                  for (var k = 0; k < arr2.length; k++) {
                    API.logs.push({out:arr2[k],agent:arr1Object.user_agent,path:arr1.path});
                  }
                };
              };
            };
            API.out({datas:{name:'static',append:'手机: '+(API.logs[0].agent.search(/iphone/i)>0?'iPhone':'android')}});
            API.out({datas:{name:'static',append:'日志条数: '+API.logs.length}});
            API.out({datas:{name:'static',append:'持续时间: '+ (API.logs[API.logs.length-1].out.timestamp-API.logs[0].out.timestamp)/3600 + '小时' }});
            API.out({datas:[{name:'time',text:'记录时间: '+(new Date(API.logs[0].out.timestamp)).toLocaleString()}]});
          }
      });
    };
    if (data.click == 'play') {
      API.out({datas:{name:'static',append:'注意：界面仅供参考'}});
      API.index = 0;
      API.run();
    };
    if (data.click == 'playNext') {
      API.index ++;
      API.run();
    };
    if (data.click == 'playLast') {
      API.index --;
      API.run();
    };
  };
  API.run = function(){
    var log = API.logs[API.index];
    API.out({datas:[{name:'time',text:'记录时间: '+(new Date(log.out.timestamp)).toLocaleString()}]});
    if (API.currentPath != log.path) {
      API.out({datas:[{name:'web',out:{command:'load:'+log.path}}]});
      API.currentPath = log.path;
      setTimeout(function(){
        API.run();
      },1000);
      return;
    };
    if (log.out.http) {
      API.out({datas:{name:'console',append:'网络请求(模拟): '+log.out.http.url}});
      API.index++;
      setTimeout(function(){
        API.run();
      },1000);
      return;
    }else if (log.out.datas) {
      API.out({datas:{name:'console',append:'界面数据('+API.index+')'+JSON.stringify(log.out.datas)}});
      if (log.out.datas.length > 3 && log.out.datas[3].name == 'realName') {
        API.out({datas:{name:'static',append:'用户姓名:'+log.out.datas[3].text }});
      };
    }else if (log.out.command) {
      if (typeof(log.out.command) === 'string') {
        API.out({datas:{name:'console',append:'界面命令('+API.index+')'+log.out.command}});
        if (log.out.command.search(/goto:|load:|present:/i)>-1 ) {
          API.index++;
          setTimeout(function(){
            API.run();
          },1000);
          return;
        };
      }else{
        API.out({datas:{name:'console',append:'界面命令('+API.index+')'+JSON.stringify(log.out.command)}});
      }
    }else{
      if (log.out.goto) {
          API.index++;
          setTimeout(function(){
            API.run();
          },1000);
          return;
      };
      API.out({datas:{name:'console',append:'用户操作: '+ JSON.stringify(log.out)}});
    }
    try{
      API.out({datas:[{name:'web',out:log.out}]});
    }catch(e){
      API.out({datas:{name:'console',append:'无法执行日志: '+JSON.stringify(log)}});
    }
    if (API.index < API.logs.length) {
      var delayTime = API.logs[API.index+1].out.timestamp - API.logs[API.index].out.timestamp;
      if (delayTime > 10000) {
        API.out({datas:{name:'console',append:'距离下一次操作还有: '+ delayTime/1000 +'秒,您可以点击>>跳过'}});
      };
      setTimeout(function(){
        API.index++;
        API.run();
      },delayTime);
    };
  },
  API.reloadData = function(data){
      API.out({command:'stopLoading'});
  };
  var ui = {
  version:0,
  nav:{
      navigationBarHidden:true
  },
  views:
  [
    {
        class:'DRTextField',
        type:'phone',
        theme:'blue',
        text:'13760659124',//13986361009, 15253269933
        frame:{x:'15',y:'15',w:'270',h:'50'},
        placeHolder:'手机号码',
        name:'phone',
        textFieldDidEditing:{name:'phone'},
    },
    {
        class:'DRButton',
        name:'query',
        DRStyle:'B8',
        enable:true,
        frame:{x:'290',y:'15',w:'40',h:'40'},
        title:'查询',
        click:{click:'query'}
    },
    {
      class:'HeroWebView',
      name:'web',
      url:host+'/cash.html',
      frame:{x:'0',y:'0',w:'320',h:'570'},
      center:{x:'170',y:'0.5x'} 
    },
    {
      class:'HeroTextView',
      name:'static',
      enable:false,
      size:22,
      backgroundColor:'eeeeee',
      cornnerRadius:5,
      frame:{x:'340',y:'50',w:'300',h:'200'},
    },
    {
      class:'HeroTextView',
      name:'console',
      backgroundColor:'eeeeee',
      frame:{x:'340',y:'260',r:'10',b:'0'},
    },
    {
        class:'DRButton',
        name:'playLast',
        DRStyle:'B8',
        enable:true,
        frame:{x:'650',y:'210',w:'40',h:'40'},
        title:'<<',
        click:{click:'playLast'}
    },
    {
        class:'DRButton',
        name:'play',
        DRStyle:'B8',
        enable:true,
        frame:{x:'700',y:'210',w:'40',h:'40'},
        title:'播放',
        click:{click:'play'}
    },
    {
        class:'DRButton',
        name:'playNext',
        DRStyle:'B8',
        enable:true,
        frame:{x:'750',y:'210',w:'40',h:'40'},
        title:'>>',
        click:{click:'playNext'}
    },
    {
      class:'HeroLabel',
      name:'time',
      size:24,
      frame:{x:'650',y:'120',r:'10',h:'40'}
    },
    {
      class:'HeroToast',
      name:'toast',
    }
  ]
};
</script>
</head>
</html>
